package cn.edu.suda.vote.vote;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.suda.vote.bean.VoteInfo;
import cn.edu.suda.vote.util.MyAdapter;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ResultActivity extends AppCompatActivity {

    private final static int MSG_SUCCESS = 1;

    private List<VoteInfo> voteInfos = new ArrayList<>();
    private MyAdapter myAdapter;

    private SwipeRefreshLayout swipeRefreshLayout;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                myAdapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        /**
         * guozhenya
         * 2016.6.9
         * 下拉刷新
         */
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_result);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                swipeRefreshLayout.setRefreshing(true);
                voteInfos.clear();
                getResult();

            }
        });

        myAdapter = new MyAdapter(this, voteInfos);
        final ListView listView = (ListView) findViewById(R.id.result_listView);
        listView.setAdapter(myAdapter);

        /**
         * guozhenya
         * 2016.6.9
         * ListView上拉加载
         */
        /*listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                if (firstVisibleItem + visibleItemCount == totalItemCount) {
                    getResult();
                }

            }
        });*/

        getResult();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void getResult() {
        String url = "http://192.168.2.100:8080/androidServer/getResult";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
                List<VoteInfo> lists = gson.fromJson(response.body().string(), new TypeToken<List<VoteInfo>>() {
                }.getType());
                voteInfos.addAll(lists);
                handler.obtainMessage(MSG_SUCCESS).sendToTarget();

            }
        });
    }

}
